

// 封装
Array.prototype.myForEach = function (fn) {
  var arr = this
  var len = arr.length
  var arg2 = arguments[1] || window;
  console.log(arg2);
  for (var i = 0; i < len; i++) {
    fn.apply(arg2, [arr[i], i, arr])
  }
}
HTMLCollection.prototype.myForEach = function (fn) {
  var arr = this
  var len = arr.length
  var arg2 = arguments[1] || window;
  console.log(arg2);
  for (var i = 0; i < len; i++) {
    fn.apply(arg2, [arr[i], i, arr])
  }
}
// data.myForEach(function (item, index, arr) {
//   this[index].innerHTML = index + '.' + item.msg
// }, $li)

//eg1
// var filterArr = data.filter(function (item, index, arr) {
//   return item.id === "3"
// })

//eg2
// var filterArr = data.filter(function (item, index, arr) {
//   return item.msg = this.name + item.msg
// }, {
//   name: 'this is test '
// })
// console.log(filterArr);


// filter 封装
Array.prototype.myFilter = function (fn) {
  var arr = this,
    len = arr.length,
    arg2 = arguments[1] || window
  nArr = [],
    nItem = '';
  for (var i = 0; i < len; i++) {
    nItem = deepClone(arr[i], {})
    fn.apply(arg2, [nItem, i, arr]) ? nArr.push(nItem) : ''
  }
  return nArr
}
HTMLCollection.prototype.myFilter = function (fn) {
  var arr = this,
    len = arr.length,
    arg2 = arguments[1] || window
  nArr = [],
    nItem = '';
  for (var i = 0; i < len; i++) {
    nItem = deepClone(arr[i], {})
    fn.apply(arg2, [nItem, i, arr]) ? nArr.push(nItem) : ''
  }
  return nArr
}
// var nArr = data.myFilter(function (item, value, arr) {
//   item.msg = this.name + item.msg
//   return item.id === "3"
// }, {
//   name: 'this is a test '
// })

Array.prototype.myMap = function (fn) {
  var arr = this,
    len = arr.length,
    arg2 = arguments[1] || window
  nArr = [], nItem = '';
  for (var i = 0; i < len; i++) {
    nItem = deepClone(arr[i], {})
    nArr.push(fn.apply(arg2, [nItem, i, arr]))
  }
  return nArr
}

// var nArr = data.myMap(function (item, index, value) {
//   item.msg = this.title + item.msg
//   return item
// }, {
//   title: "this is a test "
// })

function deepClone(obj, des) {
  var des = des || {},
    toStr = Object.prototype.toString,
    arrType = '[object Array]',
    hasOwn = Object.prototype.hasOwnProperty
  for (var key in obj) {
    if (hasOwn.call(obj, key)) {
      if (typeof obj[key] === 'object') {
        des[key] = toStr.call(obj[key]) === arrType ? [] : {}
        deepClone(obj[key], des[key])
      } else {
        des[key] = obj[key]
      }
    }
  }
  return des
}


function tplReg() {
  return /{{(.*?)}}/
}